# -*- coding: utf-8 -*-
# @Time : 2022/1/4 19:44
# @Author : Limusen
# @File : browser_utils
# 　封装驱动类

import os
import sys
import warnings
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from common.config_utils import local_config
from common.log_utils import logger

current_path = os.path.dirname(os.path.abspath(__file__))
system_driver = sys.platform


class BrowserUtils:

    def __init__(self):
        # 去除控制台警告
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        self.driver_name = local_config.driver_name

    def get_driver(self):
        if self.driver_name.lower() == 'chrome':
            logger.info("当前正在打开：%s" % self.driver_name)
            return self.__get_chrome_driver()
        elif self.driver_name.lower() == 'firefox':
            logger.info("当前正在打开：%s" % self.driver_name)
            return self.__get_firefox_driver()
        elif self.driver_name.lower() == 'edge':
            logger.info("当前正在打开：%s" % self.driver_name)
            return self.__get_edge_driver()

    def __get_chrome_driver(self):
        # 先封装简易代码
        # 加入系统环境判断
        if system_driver.lower() == "darwin":
            chrome_options = Options()
            chrome_options.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
            chrome_options.add_argument('lang=zh_CN.UTF-8')  # 设置默认编码为utf-8
            chrome_options.add_experimental_option('useAutomationExtension', False)  # 取消chrome受自动控制提示
            chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])  # 取消chrome受自动控制提示
            """如果是mac系统执行这个驱动"""
            chrome_path = os.path.join(current_path, '..','webdriver', 'chrome', 'chromedriver')
            driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
            return driver
        elif system_driver.lower() == "win32":
            chrome_options = Options()
            chrome_options.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
            chrome_options.add_argument('lang=zh_CN.UTF-8')  # 设置默认编码为utf-8
            chrome_options.add_experimental_option('useAutomationExtension', False)  # 取消chrome受自动控制提示
            chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])  # 取消chrome受自动控制提示
            chrome_path = os.path.join(current_path, '..', 'webdriver', 'chrome', 'chromedriver93.exe')
            driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
            return driver
        elif system_driver.lower() == "linux":
            chrome_options = Options()
            chrome_options.add_argument('no-sandbox')
            chrome_options.add_argument("headless")  # => 为Chrome配置无头模式
            chrome_options.add_argument('disable-dev-shm-usage')
            chrome_options.add_argument('disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
            driver = webdriver.Chrome(options=chrome_options)
            return driver

    def __get_firefox_driver(self):
        if system_driver.lower() == "darwin":
            firefox_path = os.path.join(current_path, '..', 'webdriver', 'firefox', 'geckodriver')
            driver = webdriver.Firefox(executable_path=firefox_path)
            logger.info('初始化Firefox浏览器并启动')
            return driver
        else:
            firefox_path = os.path.join(current_path, '..', 'webdriver', 'firefox', 'geckodriver.exe')
            driver = webdriver.Firefox(executable_path=firefox_path)
            logger.info('初始化Firefox浏览器并启动')
            return driver

    def __get_edge_driver(self):
        """
        驱动下载地址：https://developer.microsoft.com/zh-cn/microsoft-edge/tools/webdriver/
        :return:
        """
        edge_path = os.path.join(current_path, '..', 'webdriver', 'edge', 'msedgedriver.exe')
        driver = webdriver.Edge(executable_path=edge_path)
        logger.info('初始化Edge浏览器并启动')
        return driver


if __name__ == '__main__':
    driver = BrowserUtils().get_driver()
    driver.get("https://www.baidu.com")
    print(driver.title)
    driver.quit()
